package com.zhangshuo.patternDesign.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by shuo.zhang on 2017-10-17.
 */
public class ConnectionProxy implements InvocationHandler {


    private Connection connection;


    public ConnectionProxy(Connection connection) {
        this.connection = connection;
    }

    private void before(){
        System.out.println("方法执行前");
    }

    private void after(){
        System.out.println("方法执行后");
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        before();

        System.out.println(proxy.getClass().getName());

        Object result = method.invoke(connection,args);

        after();




        return result;
    }


    public  Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),connection.getClass().getInterfaces(),this);
    }

    public static void main(String[] args){

        Connection connectionProxy = (Connection) new ConnectionProxy(new JdbcConnection()).getProxy();


        connectionProxy.methodA();

        System.out.println(connectionProxy.getClass().getName());

    }
}
